package com.yj.rselasticsearch.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yj.common.core.domain.AjaxResult;
import com.yj.common.core.domain.entity.FileUploadLog;
import com.yj.common.utils.SecurityUtils;
import com.yj.common.utils.StringUtils;
import com.yj.rselasticsearch.domain.dto.FileUploadLogDto;
import com.yj.rselasticsearch.service.FileUploadLogService;
import com.yj.rselasticsearch.service.impl.FileUploadLogServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (file_upload_log)表控制层
 *
 * @author luoy
 */
@RestController
@RequestMapping("/fileUploadLog")
public class FileUploadLogController {
    /**
     * 服务对象
     */
    @Resource
    private FileUploadLogService fileUploadLogService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public FileUploadLog selectOne(Integer id) {
        return fileUploadLogService.selectByPrimaryKey(id);
    }

    /**
     * 查询文件上传日志
     *
     * @return
     */
    @PostMapping("list")
    public AjaxResult queryFileLog(@RequestBody FileUploadLogDto fileUploadLogDto) {
        IPage<FileUploadLog> fileUploadLogIPage = new Page<>(fileUploadLogDto.getPageIndex(), fileUploadLogDto.getPageSize());
        fileUploadLogIPage = fileUploadLogService.page(fileUploadLogIPage, new LambdaQueryWrapper<FileUploadLog>().
                like(!StringUtils.isEmpty(fileUploadLogDto.getFileName()), FileUploadLog::getFileName, fileUploadLogDto.getFileName()).
                eq(FileUploadLog::getCreateBy, SecurityUtils.getLoginUser().getUsername()));
        return AjaxResult.success(fileUploadLogIPage);
    }


    /**
     * 根据id删除日志
     * @param id
     * @return
     */
    @GetMapping("delete/{id}")
    public AjaxResult deleteLogById(@PathVariable Integer id){
        Integer result = fileUploadLogService.deleteByPrimaryKey(id);
        if (result>0){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

}
